//
//  M1Founction.m
//  NewMarryU
//
//  Created by apple on 2018/7/23.
//  Copyright © 2018年 Cedar. All rights reserved.
//

#import "M1Founction.h"

@implementation M1Founction
+(NSString *)m1_dateFormTimeStamp:(NSInteger)timeStamp{
    NSDateFormatter *m1_dateFormatter = [[NSDateFormatter alloc] init];
    [m1_dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:@"Asia/Shanghai"]];
    NSDate *assignedDateVal=[NSDate dateWithTimeIntervalSince1970:timeStamp];
    [m1_dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSString *m1_dateStr = [m1_dateFormatter stringFromDate:assignedDateVal];
    @try {
        NSDate * nowDate = [NSDate date];
        NSDate * needFormatDate = [m1_dateFormatter dateFromString:m1_dateStr];
        NSTimeInterval time = [nowDate timeIntervalSinceDate:needFormatDate];
        NSString *dateStr = [[NSString alloc] init];
        if (time<=60) {
            dateStr = @"刚刚";
        }else if(time<=60*60){
            int mins = time/60;
            dateStr = [NSString stringWithFormat:@"%d分钟前",mins];
        }else if(time<=60*60*24*2){
            [m1_dateFormatter setDateFormat:@"YYYY-MM-dd"];
            NSString * need_yMd = [m1_dateFormatter stringFromDate:needFormatDate];
            NSString *now_yMd = [m1_dateFormatter stringFromDate:nowDate];
            [m1_dateFormatter setDateFormat:@"HH:mm"];
            if ([need_yMd isEqualToString:now_yMd]) {
                dateStr = [NSString stringWithFormat:@"%@",[m1_dateFormatter stringFromDate:needFormatDate]];
            }else{
                dateStr = [NSString stringWithFormat:@"昨天 %@",[m1_dateFormatter stringFromDate:needFormatDate]];
            }
        }else {
            [m1_dateFormatter setDateFormat:@"yyyy"];
            NSString * yearStr = [m1_dateFormatter stringFromDate:needFormatDate];
            NSString *nowYear = [m1_dateFormatter stringFromDate:nowDate];
            if ([yearStr isEqualToString:nowYear]) {
                [m1_dateFormatter setDateFormat:@"MM-dd"];
                dateStr = [m1_dateFormatter stringFromDate:needFormatDate];
            }else{
                [m1_dateFormatter setDateFormat:@"yyyy-MM-dd"];
                dateStr = [m1_dateFormatter stringFromDate:needFormatDate];
            }
        }
        return dateStr;
    }
    @catch (NSException *exception) {
        return @"";
    }
}
UIImage *m1_FixImageOrientation(UIImage *originImage){
    if (originImage.imageOrientation == UIImageOrientationUp)
        return originImage;
    CGAffineTransform m1_transform = CGAffineTransformIdentity;
    switch (originImage.imageOrientation) {
        case UIImageOrientationDown:
        case UIImageOrientationDownMirrored:
            m1_transform = CGAffineTransformTranslate(m1_transform, originImage.size.width, originImage.size.height);
            m1_transform = CGAffineTransformRotate(m1_transform, M_PI);
            break;
        case UIImageOrientationLeft:
        case UIImageOrientationLeftMirrored:
            m1_transform = CGAffineTransformTranslate(m1_transform, originImage.size.width, 0);
            m1_transform = CGAffineTransformRotate(m1_transform, M_PI_2);
            break;
        case UIImageOrientationRight:
        case UIImageOrientationRightMirrored:
            m1_transform = CGAffineTransformTranslate(m1_transform, 0, originImage.size.height);
            m1_transform = CGAffineTransformRotate(m1_transform, -M_PI_2);
            break;
        default:
            break;
    }
    switch (originImage.imageOrientation) {
        case UIImageOrientationUpMirrored:
        case UIImageOrientationDownMirrored:
            m1_transform = CGAffineTransformTranslate(m1_transform, originImage.size.width, 0);
            m1_transform = CGAffineTransformScale(m1_transform, -1, 1);
            break;
        case UIImageOrientationLeftMirrored:
        case UIImageOrientationRightMirrored:
            m1_transform = CGAffineTransformTranslate(m1_transform, originImage.size.height, 0);
            m1_transform = CGAffineTransformScale(m1_transform, -1, 1);
            break;
        default:
            break;
    }
    CGContextRef ctx = CGBitmapContextCreate(NULL, originImage.size.width, originImage.size.height,
                                             CGImageGetBitsPerComponent(originImage.CGImage), 0,
                                             CGImageGetColorSpace(originImage.CGImage),
                                             CGImageGetBitmapInfo(originImage.CGImage));
    CGContextConcatCTM(ctx, m1_transform);
    switch (originImage.imageOrientation) {
        case UIImageOrientationLeft:
        case UIImageOrientationLeftMirrored:
        case UIImageOrientationRight:
        case UIImageOrientationRightMirrored:
            CGContextDrawImage(ctx, CGRectMake(0,0,originImage.size.height,originImage.size.width), originImage.CGImage);
            break;
        default:
            CGContextDrawImage(ctx, CGRectMake(0,0,originImage.size.width,originImage.size.height), originImage.CGImage);
            break;
    }
    CGImageRef cgimg = CGBitmapContextCreateImage(ctx);
    UIImage *img = [UIImage imageWithCGImage:cgimg];
    CGContextRelease(ctx);
    CGImageRelease(cgimg);
    return img;
}
@end
